package com.zjpavt.client.udp.artnet.controlPanel;

/**
 * Art-Net 协议的灯光控制台
 * @author zyc
 */
public abstract class AbstractArtNetController implements ArtnetController {
    /**
     * art-net 数据包长度
     */
    protected static final int ART_NET_PACKET_LENGTH = 530;
    /**
     * 用于控制字段的长度
     */
    protected static final int ART_NET_CONTROL_BYTE_LENGTH = 18;
    /**
     * 用于数据字段的长度
     */
    protected static final int ART_NET_DATA_BYTE_LENGTH = 512;
    /**
     * 设备类型 字符串表示
     */
    private String type;
    /**
     * 取值范围0-255 对应设备的255个域
     */
    protected int externstart;

    /**
     * 起始通道的编号 从0开始
     */
    protected int beginChannelIndex = 0;

    /**
     * 占据的通道长度
     */
    protected int channelLength = 12;

    /**
     * 解析速率 每帧(一个udp数据包)解析一次数据
     */
    protected int speed = 10;

    public AbstractArtNetController(int externstart) {
        this.externstart = externstart;
    }
    public AbstractArtNetController(int externstart, String type) {
        this.externstart = externstart;
        this.type = type;
    }
    /**
     *
     * @param beginChannelIndex 设置有效信息 第一个通道号 0-511
     * @return
     */
    protected AbstractArtNetController setBeginChannelIndex(int beginChannelIndex) {
        if (beginChannelIndex + channelLength > ART_NET_PACKET_LENGTH) {
            return this;
        }
        this.beginChannelIndex = beginChannelIndex;
        return this;
    }

    /**
     *
     * @param channelLength 设置有效信息 占用通道长度 0-512
     * @return
     */
    protected AbstractArtNetController setChannelLength(int channelLength) {
        if (beginChannelIndex + channelLength > ART_NET_PACKET_LENGTH) {
            return this;
        }
        this.channelLength = channelLength;
        return this;
    }

    protected void setSpeed(int speed) {
        this.speed = speed;
    }

    protected void setExternstart(int externstart) {
        this.externstart = externstart;
    }

    /**
     * 是否准备就绪 初始化完成 不一定每个都
     * @return
     */
    protected boolean isReady() {
        return this.channelLength != 0 && beginChannelIndex != 0;
    }

    /**
     * 筛选是否为需要的数据包
     * 截取出需要的数据
     *
     * 例如截取出第三个域的500-511通道 作为控制当前设备回路状态的通道
     * @param data
     * @return
     */
    public abstract byte[] getControlData(byte[] data);

    /**
     * 是否为符合期望的数据包
     * 为当前域
     * @param datagramPacket
     * @return
     */
    protected abstract boolean isValid(byte[] datagramPacket);

    @Override
    public String toString() {
        return "AbstractArtNetController{" +
               "type='" + type + '\'' +
               ", externstart=" + externstart +
               ", beginChannelIndex=" + beginChannelIndex +
               ", channelLength=" + channelLength +
               ", speed=" + speed +
               '}';
    }

    public String getType() {
        return type;
    }

    public int getExternstart() {
        return externstart;
    }

    public int getBeginChannelIndex() {
        return beginChannelIndex;
    }

    public int getChannelLength() {
        return channelLength;
    }

    public int getSpeed() {
        return speed;
    }


}
